package com.hivis.websocket;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.core.task.TaskExecutor;

@Component
public class MessageQueueHandler implements ApplicationContextAware {

    private final static Logger sysLogger = LoggerFactory.getLogger("system");
    private final static Logger iotMessageLogger = LoggerFactory.getLogger("iotMessage");
    private final static Logger bizMessageLogger = LoggerFactory.getLogger("bizMessage");

    // 存放所有消息的元信息
    private Map<String, JSONObject> messageMetadata = new ConcurrentHashMap<>();
    // 存放messageMetadata的key，以供发送线程发送
    private ConcurrentLinkedQueue<String> readyMessage = new ConcurrentLinkedQueue<String>();

    @Resource(name = "taskExecutor")
    TaskExecutor myThreadPool;

    @Autowired
    MessageSendTask messageSendTask;

    private boolean exit = false;

    @PostConstruct
    public void preSetData() {
    }

    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException {

        //启动接收到的NB缓存数据处理线程
        myThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                handleNBMessge();
            }
        });

    }

    @PreDestroy
    public void dostory() { exit = true;
    }

    private void handleNBMessge() {

        exit = false; // 确保服务重启时为false
        Thread.currentThread().setName("Thread-MessageQueueHandler-handleNBMessge");
        sysLogger.info("Thread-MessageQueueHandler-handleNBMessge start");


        sysLogger.info("Thread-MessageQueueHandler-handleNBMessge exit");
    }

    public void add(String key, JSONObject dataJSON) {
        // 需先保存元数据，再保存key,否则有可能轮询线程先读出Key,但无元数据
        messageMetadata.put(key, dataJSON);
        readyMessage.offer(key);
    }


    class DeviceStatus {
        private String status; // 设备在线状态
        private Date lastReportTime; // 上次上报数据时间

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public Date getLastReportTime() {
            return lastReportTime;
        }

        public void setLastReportTime(Date lastReportTime) {
            this.lastReportTime = lastReportTime;
        }

    }

}
